#pragma once

#include "../includeStd.h"

class StringUtil
{
public:
    static vector<string> split(const string& text, char seperator)
    {
        vector<string> result;
        int i = 0;
        int startIndex = 0, endIndex = 0;
        while (i <= text.size()) {
            if (text[i] == seperator || i == text.size()) {
                endIndex = i;
                if (endIndex > startIndex) {
                    string subStr = "";
                    subStr.append(text, startIndex, endIndex - startIndex);
                    result.push_back(subStr);
                }
                startIndex = endIndex + 1;
            }
            i++;
        }

        return result;
    }

    static void floatToString(string& text, double value)
    {
        char buffer[_MAX_U64TOSTR_BASE2_COUNT];
        int i = round(value * 1000);
        int quotient = i / 1000;
        int remainder = i % 1000;

        if (quotient > 0) {
            _itoa(quotient, buffer, 10);
            text.append(buffer, strlen(buffer));
            text.append(".");
        } else {
            text.append("0.");
        }
        _itoa(remainder, buffer, 10);
        text.append(buffer, strlen(buffer));
    }

    static double toFloat(string& text)
    {
        bool isFractional = false;
        double fracPos = 0.1;
        double value = 0.0f;
        int dir = 1;

        for (int i = 0; i < text.length(); i++) {
            const char& c = text.at(i);
            if (c == '.') {
                isFractional = true;
                continue;
            }
            if (c == '-') {
                dir = -1;
                continue;
            }
            int v = int(c) - int('0');
            if (isFractional) {
                value = value + v * fracPos;
                fracPos *= 0.1;
            } else {
                value = value * 10 + v;
            }
        }
        return dir * value;
    }

    static std::wstring toWString(const std::string& str)
    {
        int len;
        int slength = (int)str.length() + 1;
        len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), slength, 0, 0);
        wchar_t* buf = new wchar_t[len];
        MultiByteToWideChar(CP_ACP, 0, str.c_str(), slength, buf, len);
        std::wstring r(buf);
        delete[] buf;
        return r;
    }

    static std::string toString(const std::wstring& str)
    {
        int len;
        int slength = (int)str.length() + 1;
        len = WideCharToMultiByte(CP_ACP, 0, str.c_str(), slength, 0, 0, 0, 0);
        char* buf = new char[len];
        WideCharToMultiByte(CP_ACP, 0, str.c_str(), slength, buf, len, 0, 0);
        std::string r(buf);
        delete[] buf;
        return r;
    }
};
